<?php

/**
 * SfAudit filter form.
 *
 * @package    tercermilenio
 * @subpackage filter
 * @author     Your name here
 */
class SfAuditFormFilter extends BaseSfAuditFormFilter {

    /**
     * Define configure function
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     */
    public function configure() {
        $this->widgetSchema['remote_ip_address']->setOption('with_empty', false);
        $this->widgetSchema['object']->setOption('with_empty', false);
        $this->widgetSchema['object_key']->setOption('with_empty', false);
        $this->widgetSchema['object_changes']->setOption('with_empty', false);
        $this->widgetSchema['query']->setOption('with_empty', false);
        $this->widgetSchema['user']->setOption('with_empty', false);
        $this->widgetSchema['type']->setOption('with_empty', false);

        $defaults = $this->getDefaults();
    }

    /**
     * Redefine setup function
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     */
    public function setup() {
        parent::setup();

        $this->setWidgets(array(
            'remote_ip_address' => new sfWidgetFormFilterInput(),
            'object' => new sfWidgetFormFilterInput(),
            'object_key' => new sfWidgetFormFilterInput(),
            'object_changes' => new sfWidgetFormFilterInput(),
            'query' => new sfWidgetFormFilterInput(),
            'user' => new sfWidgetFormFilterInput(),
            'type' => new sfWidgetFormFilterInput(),
            'created_at' => new sfWidgetFormFilterDate(
                    array(
                        'from_date' => new sfWidgetFormJQueryDate(array('format' => '%day%/%month%/%year%')),
                        'to_date' => new sfWidgetFormJQueryDate(array('format' => '%day%/%month%/%year%')),
                        'with_empty' => false,
                        'template' => 'Desde %from_date% <br/>Hasta %to_date%',
            )),
        ));

        $this->setValidators(array(
            'remote_ip_address' => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
            'object' => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
            'object_key' => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
            'object_changes' => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
            'query' => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
            'user' => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
            'type' => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
            'created_at' => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
            'audit_day' => new sfValidatorBoolean(),));

        $this->widgetSchema->setNameFormat('sf_audit_filters[%s]');

        $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
    }

    /**
     * addCreatedAtColumnCriteria function
     * Specifies the criteria that applies for the field CREATED_AT
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addCreatedAtColumnCriteria(Criteria $criteria, $field, $values) {

        $name = sfAuditPeer::CREATED_AT;

        //Fix Day hours
        $beginDayTime = "00:00:00";
        $endDayTime = "23:59:59";

        //Fix ANSI
        if (!empty($values['from'])) {
            $fdate = $values['from'] . " " . $beginDayTime;
            $criteria->addAnd($name, $fdate, Criteria::GREATER_EQUAL);
        }
        //Fix ANSI
        if (!empty($values['to'])) {
            $todate = $values['to'] . " " . $endDayTime;
            $criteria->addAnd($name, $todate, Criteria::LESS_EQUAL);
        }
    }

}
